package com.example.JavaWebApi.service.impl.systemImpl;

import com.example.JavaWebApi.core.apiinterceptors.RequestHeaderContext;
import com.example.JavaWebApi.core.commonmodel.Exceptions.BusinessException;
import com.example.JavaWebApi.core.commonmodel.ResponseTypeEnum;
import com.example.JavaWebApi.model.configmodel.TencentModel;
import com.example.JavaWebApi.model.entity.systemEntity.Userinfo;
import com.example.JavaWebApi.model.vo.systemVo.CreatePersonVo;
import com.example.JavaWebApi.service.systemService.IFaceDetectionService;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.iai.v20200303.IaiClient;
import com.tencentcloudapi.iai.v20200303.models.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.MessageFormat;
import java.util.List;

/**
 * @program: sdsf_api
 * @description:
 * @author: 杨泽
 * @create: 2023-06-26 17:07
 */
@Service
@Slf4j
public class FaceDetectionServiceImpl implements IFaceDetectionService {
    @Autowired
    private TencentModel tencentModel;
    @Override
    public boolean createGroup() {
        IaiClient client=createIaiClient();
        CreateGroupRequest request=new CreateGroupRequest();
        request.setGroupName("test");
        request.setGroupId(tencentModel.getGroupId());
        request.setFaceModelVersion("3.0");
        try {
            client.CreateGroup(request);
        } catch (TencentCloudSDKException e) {
            log.error(MessageFormat.format("errCode:{0},errMsg:{1}",e.getErrorCode(),e.getMessage()));
            return false;
        }
        return true;
    }

    @Override
    public boolean createPerson(CreatePersonVo createPersonVo) {
        //Userinfo userinfo= RequestHeaderContext.getUserinfo();
        IaiClient client = createIaiClient();
        CreatePersonRequest request = new CreatePersonRequest();
        request.setGroupId(tencentModel.getGroupId());
        request.setPersonId(createPersonVo.getPersonId().toString());
        request.setPersonName(createPersonVo.getPersonName());
        request.setImage(createPersonVo.getFace());
        try {
            CreatePersonResponse response = client.CreatePerson(request);
            return true;
        } catch (TencentCloudSDKException e) {
            log.error(MessageFormat.format("errCode:{0},errMsg:{1}", e.getErrorCode(), e.getMessage()));
            return false;
        }
    }

    @Override
    public boolean addFaces(List<String> faces) {
        Userinfo userinfo= RequestHeaderContext.getUserinfo();
        IaiClient client=createIaiClient();
        CreateFaceRequest request=new CreateFaceRequest();
        request.setPersonId(userinfo.getId().toString());
        request.setImages(faces.toArray(new String[faces.size()]));
        try {
            client.CreateFace(request);
        } catch (TencentCloudSDKException e) {
            log.error(MessageFormat.format("errCode:{0},errMsg:{1}",e.getErrorCode(),e.getMessage()));
        }
        return true;
    }

    @Override
    public SearchPersonsResponse searchPersonByPic(String picture) {
        String[] arr = new String[1];
        arr[0] = tencentModel.getGroupId();
        IaiClient client = createIaiClient();
        SearchPersonsRequest request = new SearchPersonsRequest();
        request.setImage(picture);
        request.setGroupIds(arr);
        request.setMinFaceSize(80L);
        request.setMaxFaceNum(10L);
        try {
            SearchPersonsResponse response = client.SearchPersons(request);
            return response;
        } catch (TencentCloudSDKException e) {
            log.error(MessageFormat.format("errCode:{0},errMsg:{1}", e.getErrorCode(), e.getMessage()));
            throw new BusinessException(ResponseTypeEnum.FailLogin);
        }
    }

    @Override
    public SearchPersonsResponse searchPersonByUrl(String url) {
        String[] arr = new String[1];
        arr[0] = tencentModel.getGroupId();
        IaiClient client = createIaiClient();
        SearchPersonsRequest request = new SearchPersonsRequest();
        request.setUrl(url);
        request.setGroupIds(arr);
        request.setMinFaceSize(80L);
        request.setMaxFaceNum(10L);
        try {
            SearchPersonsResponse response = client.SearchPersons(request);
            return response;
        } catch (TencentCloudSDKException e) {
            log.error(MessageFormat.format("errCode:{0},errMsg:{1}", e.getErrorCode(), e.getMessage()));
            throw new BusinessException(ResponseTypeEnum.FailLogin);
        }
    }

    private Credential createCredit() {
        return new Credential(tencentModel.getSecretId(), tencentModel.getSecretKey());
    }

    private IaiClient createIaiClient() {
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setDebug(true);
        return new IaiClient(createCredit(), tencentModel.getRegion(), clientProfile);
    }
}